{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "068c2867",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9285029f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([3, 28, 28])\n",
      "torch.Size([28, 28])\n",
      "tensor(0.6420)\n"
     ]
    }
   ],
   "source": [
    "a = torch.rand(4, 3, 28, 28)\n",
    "print(a[0].shape)\n",
    "print(a[0, 0].shape)\n",
    "print(a[0, 0, 2, 4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "afbce1e1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([2, 3, 28, 28])\n",
      "torch.Size([2, 1, 28, 28])\n",
      "torch.Size([2, 2, 28, 28])\n",
      "torch.Size([2, 1, 28, 28])\n"
     ]
    }
   ],
   "source": [
    "# 切片\n",
    "print(a[:2].shape)\n",
    "\n",
    "print(a[:2, :1, :, :].shape)\n",
    "\n",
    "print(a[:2, 1:, :, :].shape)\n",
    "\n",
    "print(a[:2, -1:, :, :].shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "994a0b8d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([4, 3, 14, 14])\n",
      "torch.Size([4, 3, 14, 14])\n"
     ]
    }
   ],
   "source": [
    "# 隔行切片\n",
    "# 0:28:2  从0-28（不包括28），隔一行采样\n",
    "print(a[:, :, 0:28:2, 0:28:2].shape)\n",
    "\n",
    "print(a[:, :, ::2, ::2].shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5e753b1a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([4, 3, 28, 28])\n",
      "torch.Size([2, 3, 28, 28])\n",
      "torch.Size([4, 2, 28, 28])\n",
      "torch.Size([4, 3, 28, 28])\n",
      "torch.Size([4, 3, 8, 28])\n"
     ]
    }
   ],
   "source": [
    "# 指定索引\n",
    "print(a.shape)\n",
    "\n",
    "# 第二个参数必须是tensor\n",
    "print(a.index_select(0, torch.tensor([0, 2])).shape)\n",
    "\n",
    "print(a.index_select(1, torch.tensor([1, 2])).shape)\n",
    "\n",
    "print(a.index_select(2, torch.arange(28)).shape)\n",
    "\n",
    "print(a.index_select(2, torch.arange(8)).shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "b52b4ff2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([4, 3, 28, 28])\n",
      "torch.Size([4, 3, 28, 28])\n",
      "torch.Size([3, 28, 28])\n",
      "torch.Size([4, 28, 28])\n",
      "torch.Size([4, 3, 28, 2])\n"
     ]
    }
   ],
   "source": [
    "# ...  任意多的维度\n",
    "print(a.shape)\n",
    "\n",
    "print(a[...].shape)\n",
    "\n",
    "print(a[0, ...].shape)\n",
    "\n",
    "print(a[:, 1, ...].shape)\n",
    "\n",
    "# ... 右边的索引理解为维度的最右边\n",
    "print(a[..., :2].shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "9c7ae54d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[-0.2573,  0.4561,  0.3346,  0.0387],\n",
      "        [ 1.6873, -1.1049,  1.3294, -1.5716],\n",
      "        [ 1.6152,  0.7201,  1.7878, -1.1624]])\n",
      "tensor([[False, False, False, False],\n",
      "        [ True, False,  True, False],\n",
      "        [ True,  True,  True, False]])\n",
      "tensor([1.6873, 1.3294, 1.6152, 0.7201, 1.7878])\n",
      "torch.Size([5])\n"
     ]
    }
   ],
   "source": [
    "# mask 掩码索引\n",
    "x = torch.randn(3, 4)\n",
    "print(x)\n",
    "\n",
    "mask = x.ge(0.5)\n",
    "print(mask)\n",
    "# 取出所有mask为True（ge(0.5)）的值，结果为一维\n",
    "print(torch.masked_select(x, mask))\n",
    "\n",
    "print(torch.masked_select(x, mask).shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "af11af45",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([4, 5, 8])\n"
     ]
    }
   ],
   "source": [
    "# take 索引\n",
    "src = torch.tensor([[4, 3, 5], [6, 7, 8]])\n",
    "\n",
    "print(torch.take(src, torch.tensor([0, 2, 5])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ec80d443",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
